﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Red.GameData.Raw
{
    /// <summary>
    /// A class representing a data file in the data collection
    /// It is a container for the loaded objects. Note that the object are not bound to form a hierarchy within the file.
    /// </summary>
    public partial class DataFile
    {
        /// <summary>
        /// Get the parent collection for this file (back link)
        /// There's always only one single collection so this acts as a cheap singleton into the whole data system whenever you have at least one object :)
        /// </summary>
        public DataCollection Collection { get; private set; }

        /// <summary>
        /// Absolute path to the file, can be used to save it back or reload it.
        /// </summary>
        public string Path { get; private set; }

        /// <summary>
        /// A list of objects in this file
        /// 
        /// DATA LOSS WARNING:
        /// Don't touch it directly unless you really know what you are doing
        /// </summary>
        public List<DataObject> ObjectsInFile { get { return _objectsInFile; } }

        /// <summary>
        /// Was this file modified since it was loaded?
        /// This flag is set whenever an object from this file is modified
        /// </summary>
        public bool IsModified { get { return _isModified; } }

        /// <summary>
        /// File on the disk is newer than our version in the memory
        /// This is a serious trouble and the file should be reloaded as soon as possible. 
        /// If the file contains changes it must be flushed.
        /// NOTE: since we flush files ALWAYS in bulk this may pose a problem...
        /// </summary>
        public bool HasNewVersion { get { return _newVersionDetected; } }

        /// <summary>
        /// Create the file
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="path"></param>
        internal DataFile(DataCollection collection, string path)
        {
            Collection = collection;
            Path = path;

            _isModified = false;
            _newVersionDetected = false;
            _objectsInFile = new List<DataObject>();
            _fileTimeStamp = new DateTime(); // long ago
        }

        /// <summary>
        ///  Mark this file as modified, old habits die hard :)
        /// </summary>
        public void MarkModified()
        {
            _isModified = true;
        }

        /// <summary>
        /// We have detected a new version of the file
        /// Note: files are NOT always reloaded - for example modified files cannot be :(
        /// </summary>
        private bool _newVersionDetected;

        /// <summary>
        /// File was modified since we loaded it
        /// </summary>
        private bool _isModified;

        /// <summary>
        /// Objects in this file - use with care
        /// </summary>
        private List<DataObject> _objectsInFile;

        /// <summary>
        /// The date/time of the loaded file, used to verify automatic reloading
        /// </summary>
        private DateTime _fileTimeStamp;
    }

}
